package com.tro.mail.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.socket.TextMessage;

import com.tro.common.util.string.StringUtlis;
import com.tro.mail.dao.entity.MailAnnex;
import com.tro.mail.dao.entity.MailCc;
import com.tro.mail.dao.entity.MailCcExample;
import com.tro.mail.dao.entity.MailConsignee;
import com.tro.mail.dao.entity.MailConsigneeExample;
import com.tro.mail.dao.entity.MailExternalList;
import com.tro.mail.dao.entity.MailInbox;
import com.tro.mail.dao.entity.MailInboxExample;
import com.tro.mail.dao.entity.MailMain;
import com.tro.mail.dao.entity.MailMainExample;
import com.tro.mail.dao.entity.MailSecret;
import com.tro.mail.dao.entity.MailSecretExample;
import com.tro.mail.dao.mapper.MailAnnexMapper;
import com.tro.mail.dao.mapper.MailCcMapper;
import com.tro.mail.dao.mapper.MailConsigneeMapper;
import com.tro.mail.dao.mapper.MailExternalListMapper;
import com.tro.mail.dao.mapper.MailInboxMapper;
import com.tro.mail.dao.mapper.MailMainMapper;
import com.tro.mail.dao.mapper.MailSecretMapper;
import com.tro.mail.service.MailMainManager;
import com.tro.mail.service.MailScoketHandler;
import com.tro.mail.util.MailStatus;
import com.tro.mail.vo.InBoxVOSearch;

@Service
public class MailMainManagerImp implements MailMainManager {
	@Autowired
	MailMainMapper mapper;

	@Override
	public int countByExample(MailMainExample example) {

		return mapper.countByExample(example);
	}

	@Override
	public int deleteByExample(MailMainExample example) {

		return mapper.deleteByExample(example);
	}

	@Override
	public int deleteByPrimaryKey(Long id) {

		return mapper.deleteByPrimaryKey(id);
	}

	@Override
	public int insert(MailMain record) {

		return mapper.insert(record);
	}

	@Override
	public int insertSelective(MailMain record) {

		return mapper.insertSelective(record);
	}

	@Override
	public List<MailMain> selectByExample(MailMainExample example) {

		return mapper.selectByExample(example);
		
		//return mapper.getMails(example);
	}

	@Override
	public MailMain selectByPrimaryKey(Long id) {

		return mapper.selectByPrimaryKey(id);
	}

	@Override
	public int updateByExampleSelective(MailMain record, MailMainExample example) {

		return mapper.updateByExampleSelective(record, example);
	}

	@Override
	public int updateByExample(MailMain record, MailMainExample example) {

		return mapper.updateByExampleWithBLOBs(record, example);
	}

	@Override
	public int updateByPrimaryKeySelective(MailMain record) {

		return mapper.updateByPrimaryKeySelective(record);
	}

	@Override
	public int updateByPrimaryKey(MailMain record) {

		return mapper.updateByPrimaryKeyWithBLOBs(record);
	}

	@Override
	public List<MailMain> selectByExampleWithBLOBs(MailMainExample example) {

		return mapper.selectByExampleWithBLOBs(example);
	}

	@Override
	public int updateByExampleWithBLOBs(MailMain record, MailMainExample example) {

		return mapper.updateByExampleWithBLOBs(record, example);
	}

	@Override
	public int updateByPrimaryKeyWithBLOBs(MailMain record) {

		return mapper.updateByPrimaryKeyWithBLOBs(record);
	}

	@Autowired
	MailConsigneeMapper consigneeMapper;

	@Autowired
	MailCcMapper ccMapper;
	@Autowired
	MailSecretMapper secretMapper;

	/**
	 * 
	 * @Title: sendMail
	 * @Description: TODO(保存数据权重 密送>发送>抄送 只保存一条数据，已废弃不再用...)
	 * @param @param
	 *            record 设定文件
	 * @return void 返回类型
	 * @throws @author
	 *             leeko
	 */
	@Deprecated
	@Override
	@Transactional("mailTran")
	public void sendMail(MailMain record) {
		mapper.insert(record);
		// 获取收件人列表
		String consigneeVal = record.getConsigneeVal();
		List<Map<String, String>> consignessMap = getUsers(consigneeVal,record.getId(),record.getCreateUserId());

		// 获取抄送人列表
		String ccVal = record.getCcVal();
		List<Map<String, String>> ccMap = getUsers(ccVal,record.getId(),record.getCreateUserId());

		// 密送人列表
		String secretVal = record.getSecretVal();
		List<Map<String, String>> secretMap = getUsers(secretVal,record.getId(),record.getCreateUserId());
		MailCcExample ccExample = new MailCcExample();
		MailConsigneeExample consigneeExample = new MailConsigneeExample();
		MailSecretExample secretExample = new MailSecretExample();
		// 构造实体，存入对应表
		MailCc cc = new MailCc();
		for (Map<String, String> m : ccMap) {
			cc = new MailCc();
			cc.setMaileId(record.getId());
			cc.setSendStatus((short) 1);
			cc.setUserCsid(m.get("CSID"));
			cc.setUserName(m.get("name"));
			cc.setIsSuccess(Short.parseShort(m.get("success")));
			// 删除以前的抄送 ，防止重复抄送
			ccExample = new MailCcExample();
			ccExample.createCriteria().andMaileIdEqualTo(record.getId()).andUserCsidEqualTo(m.get("CSID"));
			ccMapper.deleteByExample(ccExample);
			ccMapper.insert(cc);
		}
		// 需要单独处理的团队数据
		String ccTeam = record.getCcTeam();
		if (StringUtils.isNotBlank(ccTeam)) {
			// 有抄送团队，团队数据样式xxxx:{1,2,3,4,5,6};xxx:{1,2,3,4,5,6,7,8};
			String[] teams = ccTeam.split(";");
			for (String t : teams) {
				// 按照:截取下
				String[] child = t.split(":{");
				if (child.length != 2) {
					// 格式有误，直接放弃掉
				} else {
					String childs = child[1].replace("}", "");
					// 再次截取，妈蛋我要疯了
					String[] ccs = childs.split(",");
					for (String c : ccs) {
						// 删除以前的抄送 ，防止重复抄送
						ccExample = new MailCcExample();
						ccExample.createCriteria().andMaileIdEqualTo(record.getId()).andUserCsidEqualTo(c);
						ccMapper.deleteByExample(ccExample);
						// 存入数据库
						cc = new MailCc();
						cc.setIsSuccess((short) 1);
						cc.setMaileId(record.getId());
						cc.setUserCsid(c);
						ccMapper.insert(cc);
					}
				}
			}
		}
		// 构造实体，存入对应表
		MailConsignee consignee = new MailConsignee();
		for (Map<String, String> m : consignessMap) {
			consignee = new MailConsignee();
			consignee.setMailId(record.getId());
			consignee.setSendStatus((short) 1);
			consignee.setUserCsid(m.get("CSID"));
			consignee.setUserName(m.get("name"));
			consignee.setIsSuccess(Short.parseShort(m.get("success")));
			// 检查抄送表以及本身
			ccExample = new MailCcExample();
			ccExample.createCriteria().andMaileIdEqualTo(record.getId()).andUserCsidEqualTo(m.get("CSID"));
			ccMapper.deleteByExample(ccExample);
			// 检查本身
			consigneeExample = new MailConsigneeExample();
			consigneeExample.createCriteria().andMailIdEqualTo(record.getId()).andUserCsidEqualTo(m.get("CSID"));
			consigneeMapper.deleteByExample(consigneeExample);
			consigneeMapper.insert(consignee);
		}
		String consigneeTeam = record.getConsigneeTeam();
		if (StringUtils.isNotBlank(consigneeTeam)) {
			// 有抄送团队，团队数据样式xxxx:{1,2,3,4,5,6};xxx:{1,2,3,4,5,6,7,8};
			String[] teams = consigneeTeam.split(";");
			for (String t : teams) {
				// 按照:截取下
				String[] child = t.split(":{");
				if (child.length != 2) {
					// 格式有误，直接放弃掉
				} else {
					String childs = child[1].replace("}", "");
					// 再次截取，妈蛋我要疯了
					String[] ccs = childs.split(",");
					for (String c : ccs) {
						// 删除以前的抄送 ，防止重复
						ccExample = new MailCcExample();
						ccExample.createCriteria().andMaileIdEqualTo(record.getId()).andUserCsidEqualTo(c);
						ccMapper.deleteByExample(ccExample);
						// 检查本身
						consigneeExample = new MailConsigneeExample();
						consigneeExample.createCriteria().andMailIdEqualTo(record.getId()).andUserCsidEqualTo(c);
						consigneeMapper.deleteByExample(consigneeExample);
						// 存入数据库
						consignee = new MailConsignee();
						consignee.setIsSuccess((short) 1);
						consignee.setMailId(record.getId());
						consignee.setUserCsid(c);
						consigneeMapper.insert(consignee);
					}
				}
			}
		}
		// 构造实体，存入对应表
		MailSecret secret = new MailSecret();
		for (Map<String, String> m : secretMap) {
			secret = new MailSecret();
			secret.setMailId(record.getId());
			secret.setSendStatus((short) 1);
			secret.setUserCsid(m.get("CSID"));
			secret.setUserName(m.get("name"));
			secret.setIsSuccess(Short.parseShort(m.get("success")));
			// 检查抄送表以及本身
			ccExample = new MailCcExample();
			ccExample.createCriteria().andMaileIdEqualTo(record.getId()).andUserCsidEqualTo(m.get("CSID"));
			ccMapper.deleteByExample(ccExample);
			// 检查本身
			consigneeExample = new MailConsigneeExample();
			consigneeExample.createCriteria().andMailIdEqualTo(record.getId()).andUserCsidEqualTo(m.get("CSID"));
			consigneeMapper.deleteByExample(consigneeExample);
			secretExample = new MailSecretExample();
			secretExample.createCriteria().andMailIdEqualTo(record.getId()).andUserCsidEqualTo(m.get("CSID"));
			secretMapper.deleteByExample(secretExample);
			secretMapper.insert(secret);
		}
		String secretTeam = record.getSecretTeam();
		if (StringUtils.isNotBlank(secretTeam)) {
			// 有抄送团队，团队数据样式xxxx:{1,2,3,4,5,6};xxx:{1,2,3,4,5,6,7,8};
			String[] teams = secretTeam.split(";");
			for (String t : teams) {
				// 按照:截取下
				String[] child = t.split(":{");
				if (child.length != 2) {
					// 格式有误，直接放弃掉
				} else {
					String childs = child[1].replace("}", "");
					// 再次截取，妈蛋我要疯了
					String[] ccs = childs.split(",");
					for (String c : ccs) {
						// 删除以前的抄送 ，防止重复
						ccExample = new MailCcExample();
						ccExample.createCriteria().andMaileIdEqualTo(record.getId()).andUserCsidEqualTo(c);
						ccMapper.deleteByExample(ccExample);
						// 检查本身
						consigneeExample = new MailConsigneeExample();
						consigneeExample.createCriteria().andMailIdEqualTo(record.getId()).andUserCsidEqualTo(c);
						consigneeMapper.deleteByExample(consigneeExample);
						secretExample = new MailSecretExample();
						secretExample.createCriteria().andMailIdEqualTo(record.getId()).andUserCsidEqualTo(c);
						secretMapper.deleteByExample(secretExample);
						// 存入数据库
						secret = new MailSecret();
						secret.setIsSuccess((short) 1);
						secret.setMailId(record.getId());
						secret.setUserCsid(c);
						secretMapper.insert(secret);
					}
				}
			}
		}
	}

	/**
	 * 
	 * @Title: getUsers
	 * @Description: TODO(根据规则获取收件人、抄送人、密送人列表,不考虑效率了，妈蛋)
	 * @param @param
	 *            consigneeVal
	 * @param @return
	 *            设定文件
	 * @return List<Map<String,String>> 返回类型
	 * @throws @author
	 *             leeko
	 */
	@Autowired
	MailExternalListMapper externalMapper;
	public List<Map<String, String>> getUsers(String consigneeVal,Long mailId,String fromUser) {
		MailExternalList external=new MailExternalList();
		List<Map<String, String>> consignees = new ArrayList<>();
		if(StringUtils.isBlank(consigneeVal)){
			return consignees;
		}
		String[] cons = consigneeVal.split(";");
		
		Map<String, String> consignee = new HashMap<>();
		for (String c : cons) {
			consignee = new HashMap<>();
			if (StringUtils.isNotBlank(c)) {
				// 如果是存在<
				if (c.indexOf("<") != -1) {
					// 按< 分割掉
					String[] sub = c.split("<");
					if (sub.length != 2) {
						continue;
					} else {
						String csid = sub[1].replace(">", "");
						if (csid.indexOf("@") != -1) {
							if(StringUtlis.isEmail(csid)){
								//格式正确的邮箱地址
								external=new MailExternalList();
								external.setFromUser(fromUser);
								external.setToUser(c);
								external.setMailId(mailId);
								external.setRetryCount(0L);
								external.setSendStatu(MailStatus.MAIL_TEMP);
								//保存到数据库中
								externalMapper.insert(external);
							}else{
								//格式不正确
							}
						} else {
							consignee.put("name", sub[0]);
							consignee.put("CSID", csid);
							consignee.put("success", "1");
						}
						// 此处怎么判断Csid是否有误--不判断了，交到前端调用接口自行判断
					}
				} else {
					// 不存在<> 只认为是csid
					if (c.indexOf("@") != -1) {
						// 发送外网邮件
						if(StringUtlis.isEmail(c)){
							//格式正确的邮箱地址
							external=new MailExternalList();
							external.setFromUser(fromUser);
							external.setToUser(c);
							external.setMailId(mailId);
							external.setRetryCount(0L);
							external.setSendStatu(MailStatus.MAIL_TEMP);
							//保存到数据库中
							externalMapper.insert(external);
						}else{
							//格式不正确
						}
						
						
					} else if (c.indexOf("[") != -1) {
						// 此处为团队 暂不处理
						continue;
					} else {
						consignee.put("name", c);
						consignee.put("CSID", c);
						consignee.put("success", "1");
						// 添加到个人通讯录..
					}
				}
				if(consignee.get("CSID")!=null&&StringUtils.isNotBlank(consignee.get("CSID").toString())){
					consignees.add(consignee);
				}
				
			}
		}
		return consignees;
	}

	// (系统用户组[sys]:{admin<admin>;}),(系统用户组[sys]:{admin<admin>;})
	public List<String> getTeamUsers(String val, String str,String fromUser,Long mailId) {
		List<String> result = new ArrayList<>();
		MailExternalList external=new MailExternalList();
		if (StringUtils.isNotBlank(val)) {
			String[] teams = val.split(",");
			for (String t : teams) {
				// 按照:截取下
				String[] child = t.split(":\\{");
				if (child.length != 2) {
					// 格式有误，直接放弃掉
				} else {
					// 判断哈是否在收件人列表中
					String team = child[0].replace("(", "");
					if (str.indexOf(team) != -1) {
						String childs = child[1].replace("}", "");
						// 再次截取，妈蛋我要疯了
						String[] ccs = childs.split(";");
						for (String c : ccs) {
							if(StringUtils.isNotBlank(c)){
								String mailAddress=getCSID(c);
								if(StringUtils.isNotBlank(mailAddress)){
									if(mailAddress.indexOf("@")!=-1){
										external=new MailExternalList();
										external.setFromUser(fromUser);
										external.setToUser(c);
										external.setMailId(mailId);
										external.setRetryCount(0L);
										external.setSendStatu(MailStatus.MAIL_TEMP);
										//保存到数据库中
										externalMapper.insert(external);
									}else{
										result.add(getCSID(c));
									}
								}
								
							}
						}
					}
				}
			}
		}
		return result;
	}

	// admin<admin>
	public String getCSID(String str) {
		if(StringUtils.isNotBlank(str)){
			String[] user=str.split("<");
			if(user.length!=2){
				return "";
			}else{
				return user[1].replace(">", "");
			}
		}
		return "";
	}

	public List<MailAnnex> getAnnex(String str, Long mailId) {
		List<MailAnnex> annexs = new ArrayList<>();
		MailAnnex bean = new MailAnnex();
		if (StringUtils.isNotBlank(str)) {
			String[] annexList = str.split(",");
			for (String an : annexList) {
				String[] m = an.split("\\|");
				if (m.length != 4) {
					// 格式有误
					continue;
				} else {
					bean = new MailAnnex();
					bean.setAnnexName(m[0]);
					bean.setAnnexPath(m[1]);
					bean.setMailId(mailId);
					bean.setAnnexSize(m[2]);
					//获取对应style
					Map<String,Object> result=StringUtlis.getStyle(m[3]);
					bean.setAnnexSuffix(m[3]);
					bean.setAnnexStyle(result.get("style").toString());
					bean.setAllowPre(Short.parseShort(result.get("allowPre").toString()));
					annexs.add(bean);
				}
			}
		}
		return annexs;
	}

	@Autowired
	MailInboxMapper inBoxMapper;
	@Autowired
	MailAnnexMapper annexMapper;
	@Autowired
	MailScoketHandler handler;
	@Override
	@Transactional("mailTran")
	public void sendMailN(MailMain record) {
		record.setForderId(MailStatus.SENT_ID);
		mapper.insert(record);
		// 获取收件人列表
		String consigneeVal = record.getConsigneeVal();
		List<Map<String, String>> consignessMap = getUsers(consigneeVal,record.getId(),record.getCreateUserId());
		// 获取抄送人列表
		String ccVal = record.getCcVal();
		List<Map<String, String>> ccMap = getUsers(ccVal,record.getId(),record.getCreateUserId());
		// 密送人列表
		String secretVal = record.getSecretVal();
		List<Map<String, String>> secretMap = getUsers(secretVal,record.getId(),record.getCreateUserId());
		MailInbox inBox = new MailInbox();
		MailInboxExample example = new MailInboxExample();
		Set<String> toUsers=new HashSet<>();//收件人列表
		for (Map<String, String> m : ccMap) {
			// 干掉已存在数据
			example = new MailInboxExample();
			example.createCriteria().andMailIdEqualTo(record.getId()).andOwerUserIdEqualTo(m.get("CSID"));
			inBoxMapper.deleteByExample(example);
			inBox = new MailInbox();
			inBox.setIsRead(MailStatus.NOT_READ);
			inBox.setMailId(record.getId());
			inBox.setMailIsReceipt(record.getIsReceipt());
			inBox.setOwerUserId(m.get("CSID"));
			inBox.setReceiveTime(new Date());
			inBox.setSceretStatu(MailStatus.NOT_SCERET);
			inBox.setForderId(MailStatus.INBOX_ID);
			toUsers.add(m.get("CSID"));
			try{
				handler.sendMessageToUser(m.get("CSID"), new TextMessage("收到一封新邮件"));
			}catch(Exception ex){
				
			}
			
			inBoxMapper.insert(inBox);
		}
		// 需要单独处理的团队数据
		String ccTeam = record.getCcTeam();
		List<String> ccTeamUsers = getTeamUsers(ccTeam,ccVal,record.getCreateUserId(),record.getId());
		// 构造实体，存入对应表
		for (Map<String, String> m : consignessMap) {
			// 干掉收件箱表中的数据
			example = new MailInboxExample();
			example.createCriteria().andMailIdEqualTo(record.getId()).andOwerUserIdEqualTo(m.get("CSID"));
			inBoxMapper.deleteByExample(example);
			inBox = new MailInbox();
			inBox.setIsRead(MailStatus.NOT_READ);
			inBox.setMailId(record.getId());
			inBox.setMailIsReceipt(record.getIsReceipt());
			inBox.setOwerUserId(m.get("CSID"));
			inBox.setReceiveTime(new Date());
			inBox.setSceretStatu(MailStatus.NOT_SCERET);
			inBox.setForderId(MailStatus.INBOX_ID);
			toUsers.add(m.get("CSID"));
			try{
				handler.sendMessageToUser(m.get("CSID"), new TextMessage("收到一封新邮件"));
			}catch(Exception ex){
				
			}
			inBoxMapper.insert(inBox);
		}
		String consigneeTeam = record.getConsigneeTeam();
		List<String> consigneeTeamUsers = getTeamUsers(consigneeTeam,consigneeVal,record.getCreateUserId(),record.getId());
		for (Map<String, String> m : secretMap) {
			example = new MailInboxExample();
			example.createCriteria().andMailIdEqualTo(record.getId()).andOwerUserIdEqualTo(m.get("CSID"));
			inBoxMapper.deleteByExample(example);
			inBox = new MailInbox();
			inBox.setIsRead(MailStatus.NOT_READ);
			inBox.setMailId(record.getId());
			inBox.setMailIsReceipt(record.getIsReceipt());
			inBox.setOwerUserId(m.get("CSID"));
			inBox.setReceiveTime(new Date());
			inBox.setSceretStatu(MailStatus.IS_SCERET);
			inBox.setForderId(MailStatus.INBOX_ID);
			toUsers.add(m.get("CSID"));
			try{
				handler.sendMessageToUser(m.get("CSID"), new TextMessage("收到一封新邮件"));
			}catch(Exception ex){
				
			}
			inBoxMapper.insert(inBox);
		}
		for (String s : ccTeamUsers) {
			example = new MailInboxExample();
			example.createCriteria().andMailIdEqualTo(record.getId()).andOwerUserIdEqualTo(s);
			inBoxMapper.deleteByExample(example);
			inBox = new MailInbox();
			inBox.setIsRead(MailStatus.NOT_READ);
			inBox.setMailId(record.getId());
			inBox.setMailIsReceipt(record.getIsReceipt());
			inBox.setOwerUserId(s);
			inBox.setReceiveTime(new Date());
			inBox.setSceretStatu(MailStatus.NOT_SCERET);
			inBox.setForderId(MailStatus.INBOX_ID);
			try{
				handler.sendMessageToUser(s, new TextMessage("收到一封新邮件"));
			}catch(Exception ex){
				
			}
			inBoxMapper.insert(inBox);
		}
		for (String s : consigneeTeamUsers) {
			example = new MailInboxExample();
			example.createCriteria().andMailIdEqualTo(record.getId()).andOwerUserIdEqualTo(s);
			inBoxMapper.deleteByExample(example);
			inBox = new MailInbox();
			inBox.setIsRead(MailStatus.NOT_READ);
			inBox.setMailId(record.getId());
			inBox.setMailIsReceipt(record.getIsReceipt());
			inBox.setOwerUserId(s);
			inBox.setReceiveTime(new Date());
			inBox.setSceretStatu(MailStatus.NOT_SCERET);
			inBox.setForderId(MailStatus.INBOX_ID);
			try{
				handler.sendMessageToUser(s, new TextMessage("收到一封新邮件"));
			}catch(Exception ex){
				
			}
			inBoxMapper.insert(inBox);
		}
		String secretTeam = record.getSecretTeam();
		List<String> secretTeamUsers = getTeamUsers(secretTeam,secretVal,record.getCreateUserId(),record.getId());
		for (String s : secretTeamUsers) {
			example = new MailInboxExample();
			example.createCriteria().andMailIdEqualTo(record.getId()).andOwerUserIdEqualTo(s);
			inBoxMapper.deleteByExample(example);
			inBox = new MailInbox();
			inBox.setIsRead(MailStatus.NOT_READ);
			inBox.setMailId(record.getId());
			inBox.setMailIsReceipt(record.getIsReceipt());
			inBox.setOwerUserId(s);
			inBox.setReceiveTime(new Date());
			inBox.setSceretStatu(MailStatus.IS_SCERET);
			inBox.setForderId(MailStatus.INBOX_ID);
			try{
				handler.sendMessageToUser(s, new TextMessage("收到一封新邮件"));
			}catch(Exception ex){
				
			}
			inBoxMapper.insert(inBox);
		}

		// 保存附件信息
		List<MailAnnex> annexs = getAnnex(record.getAnnexVal(), record.getId());
		for (MailAnnex an : annexs) {
			annexMapper.insert(an);
		}
	}
	
	@Transactional("mailTran")
	@Override
	public void delMail(String mailIds) {
		String[] ids=mailIds.split(",");
		for(String id:ids){
			MailMain mail=new MailMain();
			mail.setId(Long.parseLong(id));
			mail.setMailStatu(MailStatus.MAIL_DEL);
			mapper.updateByPrimaryKeySelective(mail);
		}
		
	}
	
	@Transactional
	@Override
	public void addStar(String mailIds) {
		String[] ids=mailIds.split(",");
		MailMain mail=new MailMain();
		MailInbox inBox=new MailInbox();
		for(String id:ids){
			mail=new MailMain();
			mail.setId(Long.parseLong(id));
			mail.setIsStart(MailStatus.MAIL_START);
			int res=mapper.updateByPrimaryKeySelective(mail);
			if(res==0){
				inBox=new MailInbox();
				inBox.setId(Long.parseLong(id));
				inBox.setIsStart(Long.parseLong("1"));
				inBoxMapper.updateByPrimaryKeySelective(inBox);
			}
		}
		
	}

	@Override
	public void unStar(String mailIds) {
		String[] ids=mailIds.split(",");
		MailMain mail=new MailMain();
		MailInbox inBox=new MailInbox();
		for(String id:ids){
			mail=new MailMain();
			mail.setId(Long.parseLong(id));
			mail.setIsStart(MailStatus.MAIL_UN_START);
			int res=mapper.updateByPrimaryKeySelective(mail);
			if(res==0){
				inBox=new MailInbox();
				inBox.setId(Long.parseLong(id));
				inBox.setIsStart(Long.parseLong("0"));
				inBoxMapper.updateByPrimaryKeySelective(inBox);
			}
		}
		
	}

	@Override
	public List<MailMain> getMails(InBoxVOSearch example) {
		return mapper.getMails(example);
	}
	
	public int getMailsCount(InBoxVOSearch example){
		return mapper.getMailsCount(example);
	}
}